package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.*;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ExportProductService;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.vo.ExportProductResult;
import cn.itcast.vo.ExportResult;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

// com.alibaba.dubbo.config.annotation.Service;
@Service(timeout = 200000) // 设置超时时间为200秒
public class ExportServiceImpl implements ExportService{

    // 注入报运单相关信息
    @Autowired
    private ExportDao exportDao;
    @Autowired
    private ExportProductDao exportProductDao;
    @Autowired
    private ExtEproductDao extEproductDao;


    // 注入购销合同相关信息
    @Autowired
    private ContractDao contractDao;
    @Autowired
    private ContractProductDao contractProductDao;
    @Autowired
    private ExtCproductDao extCproductDao;

    @Override
    public PageInfo<Export> findByPage(ExportExample exportExample, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Export> list = exportDao.selectByExample(exportExample);
        return new PageInfo<>(list);
    }

    @Override
    public Export findById(String id) {
        return exportDao.selectByPrimaryKey(id);
    }


    @Override
    public void save(Export export) {
        //1. 设置报运单id、状态、商品数量、附件数量、合同号
        export.setId(UUID.randomUUID().toString());
        export.setState(0);
        // 获取购销合同id
        String contractIds = export.getContractIds();
        // 分割
        String[] array = contractIds.split(",");
        // 数组转集合
        List<String> contractIdList = Arrays.asList(array);
        // 根据购销合同id查询
        ContractExample contractExample = new ContractExample();
        contractExample.createCriteria().andIdIn(contractIdList);
        List<Contract> contractList = contractDao.selectByExample(contractExample);
        // 获取保存合同号
        String contractNos = "";
        if (contractList != null && contractList.size()>0){
            for (Contract contract : contractList) {
                contractNos += contract.getContractNo() + " ";
                //2. 合同列表点击报运生成报运单后，要修改购销合同状态为2，表示已经生成报运
                contract.setState(2);
                contractDao.updateByPrimaryKeySelective(contract);
            }
        }
        // 设置合同号
        export.setCustomerContract(contractNos);


        //3. 根据购销合同id查询货物，把查询到的结果添加到报运单的`商品表`  (数据搬家)
        //3.1 根据购销合同id查询货物
        ContractProductExample cpExample = new ContractProductExample();
        cpExample.createCriteria().andContractIdIn(contractIdList);
        List<ContractProduct> cpList = contractProductDao.selectByExample(cpExample);

        // 定义map，存储货物id + 商品id
        Map<String,String> map = new HashMap<>();

        //3.2 把查询到的结果添加到报运单的`商品表`  (数据搬家)
        if (cpList != null && cpList.size() > 0) {
            // 遍历货物
            for (ContractProduct contractProduct : cpList) {
                // 创建并封装商品对象
                ExportProduct exportProduct = new ExportProduct();
                // 对象拷贝 org.springframework.beans.BeanUtils;
                BeanUtils.copyProperties(contractProduct,exportProduct);
                // 设置商品id、商品关联的报运单id
                exportProduct.setId(UUID.randomUUID().toString());
                exportProduct.setExportId(export.getId());
                // 添加商品
                exportProductDao.insertSelective(exportProduct);

                // 保存货物id + 商品id到map集合
                map.put(contractProduct.getId(),exportProduct.getId());
            }
        }


        //4. 根据购销合同id查询货物附件，把查询到的结果添加到报运单的`商品附件表`(数据搬家)
        //4.1 根据购销合同id查询货物附件
        ExtCproductExample extCproductExample = new ExtCproductExample();
        extCproductExample.createCriteria().andContractIdIn(contractIdList);
        List<ExtCproduct> extCproductList = extCproductDao.selectByExample(extCproductExample);

        //4.2 把查询到的结果添加到报运单的`商品附件表`(数据搬家)
        if (extCproductList != null && extCproductList.size() > 0) {
            // 遍历货物附件
            for (ExtCproduct extCproduct : extCproductList) {
                // 创建并封装商品附件
                ExtEproduct extEproduct = new ExtEproduct();
                BeanUtils.copyProperties(extCproduct,extEproduct);
                // 设置商品附件id、关联的商品id、关联的报运单id
                extEproduct.setId(UUID.randomUUID().toString());
                // 根据货物id获取商品id
                String exportProductId = map.get(extCproduct.getContractProductId());
                extEproduct.setExportProductId(exportProductId);
                extEproduct.setExportId(export.getId());

                // 保存商品附件
                extEproductDao.insertSelective(extEproduct);
            }
        }

        // 设置商品数量
        export.setProNum(cpList.size());
        // 设置附件数量
        export.setExtNum(extCproductList.size());

        //5. 添加`报运单`
        exportDao.insertSelective(export);
    }

    @Override
    public void update(Export export) {
        //1. 修改报运单
        exportDao.updateByPrimaryKeySelective(export);

        //2. 修改报运单的商品
        List<ExportProduct> exportProducts = export.getExportProducts();
        if (exportProducts != null && exportProducts.size() > 0) {
            for (ExportProduct exportProduct : exportProducts) {
                exportProductDao.updateByPrimaryKeySelective(exportProduct);
            }
        }
    }

    @Override
    public void delete(String id) {
        exportDao.deleteByPrimaryKey(id);
    }


    @Override
    public void updateExport(ExportResult exportResult) {
        //1. 根据webservice返回的结果，修改报运单（状态、备注）
                Export export = new Export();
        export.setId(exportResult.getExportId());
        export.setState(exportResult.getState());
        export.setRemark(exportResult.getRemark());
        exportDao.updateByPrimaryKeySelective(export);

        //2. 根据webservice返回的结果， 修改商品（交税金额）
        Set<ExportProductResult> products = exportResult.getProducts();
        if (products != null && products.size() > 0){
            for (ExportProductResult product : products) {
                ExportProduct exportProduct = new ExportProduct();
                exportProduct.setId(product.getExportProductId());
                exportProduct.setTax(product.getTax());
                exportProductDao.updateByPrimaryKeySelective(exportProduct);
            }
        }
    }

}




















